﻿Imports System.Data
Imports System.Data.SqlClient

Namespace DB

    ''' <summary>
    ''' TypeName:    ServiceOrderType
    ''' Author:      GeneratedCustomTemplate
    ''' DBTableName: SVC_ServiceOrders
    ''' </summary>
    ''' <remarks></remarks>

    Public Class ServiceOrderType
        Inherits DBTableObjectBase

#Region "Declarations"
        'Protected Friend iServiceOrderId as Guid
        Protected Friend iCustomerId As Guid
        Protected Friend iOrderNumber As String
        Protected Friend iOrderReceivedDate As DateTime
        Protected Friend iOrderCompletedDate As DateTime
        Protected Friend iServiceEquipmentId As Guid
        Protected Friend iServiceItemImieSerialNumber As String
        Protected Friend iEstimatedDate As DateTime
        Protected Friend iEstimatedPrice As Decimal
        Protected Friend iRegistrationFee As Decimal
        Protected Friend iActualPrice As Decimal
        Protected Friend iEstimateApproved As String
        Protected Friend iStatus As String
        Protected Friend iInDepartment As String
#End Region

#Region "Constructors"
        Public Sub New()
            MyBase.New(TableNamesEnum.SVC_ServiceOrders)
        End Sub
        Public Sub New(ByVal ServiceOrderId As Guid)
            MyBase.New(TableNamesEnum.SVC_ServiceOrders, ServiceOrderId)
        End Sub
        Public Sub New(ByVal ServiceOrderId As Guid, _
                        ByVal CustomerId As Guid, _
                        ByVal OrderNumber As String, _
                        ByVal OrderReceivedDate As DateTime, _
                        ByVal OrderCompletedDate As DateTime, _
                        ByVal EstimatedDate As DateTime, _
                        ByVal EstimatedPrice As Decimal, _
                        ByVal ActualPrice As Decimal, _
                        ByVal EstimateApproved As String, _
                        ByVal Status As String, _
                        ByVal LMUserId As Guid, _
                        ByVal LMDate As DateTime)
            MyBase.New(TableNamesEnum.SVC_ServiceOrders)
            Me.iId = ServiceOrderId
            Me.iCustomerId = CustomerId
            Me.iOrderNumber = OrderNumber
            Me.iOrderReceivedDate = OrderReceivedDate
            Me.iOrderCompletedDate = OrderCompletedDate
            Me.iEstimatedDate = EstimatedDate
            Me.iEstimatedPrice = EstimatedPrice
            Me.iActualPrice = ActualPrice
            Me.iEstimateApproved = EstimateApproved
            Me.iStatus = Status
            Me.iLMUserId = LMUserId
            Me.iLMDate = LMDate

        End Sub
#End Region

#Region "Exposed Properties"
        Public Property ServiceOrderId() As Guid
            Get
                Return Me.iId
            End Get
            Set(ByVal value As Guid)
                Me.iId = value
            End Set
        End Property
        Public Property CustomerId() As Guid
            Get
                Return Me.iCustomerId
            End Get
            Set(ByVal value As Guid)
                Me.iCustomerId = value
            End Set
        End Property
        Public Property OrderNumber() As String
            Get
                Return Me.iOrderNumber
            End Get
            Set(ByVal value As String)
                Me.iOrderNumber = value
            End Set
        End Property
        Public Property OrderReceivedDate() As DateTime
            Get
                Return Me.iOrderReceivedDate
            End Get
            Set(ByVal value As DateTime)
                Me.iOrderReceivedDate = value
            End Set
        End Property
        Public Property OrderCompletedDate() As DateTime
            Get
                Return Me.iOrderCompletedDate
            End Get
            Set(ByVal value As DateTime)
                Me.iOrderCompletedDate = value
            End Set
        End Property
        Public Property ServiceEquipmentId As Guid
            Get
                Return Me.iServiceEquipmentId
            End Get
            Set(value As Guid)
                Me.iServiceEquipmentId = value
            End Set
        End Property
        Public Property ServiceItemImieSerialNumber As String
            Get
                Return Me.iServiceItemImieSerialNumber
            End Get
            Set(value As String)
                Me.iServiceItemImieSerialNumber = value
            End Set
        End Property
        Public Property EstimatedDate() As DateTime
            Get
                Return Me.iEstimatedDate
            End Get
            Set(ByVal value As DateTime)
                Me.iEstimatedDate = value
            End Set
        End Property
        Public Property EstimatedPrice() As Decimal
            Get
                Return Me.iEstimatedPrice
            End Get
            Set(ByVal value As Decimal)
                Me.iEstimatedPrice = value
            End Set
        End Property
        Public Property RegistrationFee As Decimal
            Get
                Return Me.iRegistrationFee
            End Get
            Set(value As Decimal)
                Me.iRegistrationFee = value
            End Set
        End Property
        Public Property ActualPrice() As Decimal
            Get
                Return Me.iActualPrice
            End Get
            Set(ByVal value As Decimal)
                Me.iActualPrice = value
            End Set
        End Property
        Public Property EstimateApproved() As String
            Get
                Return Me.iEstimateApproved
            End Get
            Set(ByVal value As String)
                Me.iEstimateApproved = value
            End Set
        End Property
        Public Property Status() As String
            Get
                Return Me.iStatus
            End Get
            Set(ByVal value As String)
                Me.iStatus = value
            End Set
        End Property
        Public Property InDepartment As String
            Get
                Return Me.iInDepartment
            End Get
            Set(value As String)
                Me.iInDepartment = value
            End Set
        End Property
#End Region

#Region "Implementations"
        Protected Friend Overloads Function Load(ByVal Row As DataRow) As Boolean
            If IsDBNull(Row(0)) Then Me.iId = Guid.Empty Else Me.iId = Row(0)
            If IsDBNull(Row(1)) Then Me.iCustomerId = Nothing Else Me.iCustomerId = Row(1)
            If IsDBNull(Row(2)) Then Me.iOrderNumber = Nothing Else Me.iOrderNumber = Row(2)
            If IsDBNull(Row(3)) Then Me.iOrderReceivedDate = Nothing Else Me.iOrderReceivedDate = Row(3)
            If IsDBNull(Row(4)) Then Me.iOrderCompletedDate = Nothing Else Me.iOrderCompletedDate = Row(4)
            If IsDBNull(Row(5)) Then Me.iServiceEquipmentId = Nothing Else Me.iServiceEquipmentId = Row(5)
            If IsDBNull(Row(6)) Then Me.iServiceItemImieSerialNumber = Nothing Else Me.iServiceItemImieSerialNumber = Row(6)
            If IsDBNull(Row(7)) Then Me.iEstimatedDate = Nothing Else Me.iEstimatedDate = Row(7)
            If IsDBNull(Row(8)) Then Me.iEstimatedPrice = Nothing Else Me.iEstimatedPrice = Row(8)
            If IsDBNull(Row(9)) Then Me.iRegistrationFee = Nothing Else Me.iRegistrationFee = Row(9)
            If IsDBNull(Row(10)) Then Me.iActualPrice = Nothing Else Me.iActualPrice = Row(10)
            If IsDBNull(Row(11)) Then Me.iEstimateApproved = Nothing Else Me.iEstimateApproved = Row(11)
            If IsDBNull(Row(12)) Then Me.iStatus = Nothing Else Me.iStatus = Row(12)
            If IsDBNull(Row(13)) Then Me.iInDepartment = String.Empty Else Me.iInDepartment = Row(13)
            If IsDBNull(Row(14)) Then Me.iLMUserId = Nothing Else Me.iLMUserId = Row(14)
            If IsDBNull(Row(15)) Then Me.iLMDate = Nothing Else Me.iLMDate = Row(15)
            Return True
        End Function
        Protected Friend Overrides Function Load() As Boolean
            'sp_[TableName]Get
            Dim resBool As Boolean = True
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Get '" & Me.iId.ToString("D") & "'")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction

            Dim dt As DataTable = New DataTable
            dt = Utility.GetList(cmd)

            If dt.Rows.Count > 0 Then
                Me.Load(dt.Rows(0))
            Else
                resBool = False
            End If

            Return resBool
        End Function
        Protected Friend Overrides Function Insert() As Boolean
            'sp_[TableName]Insert
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter

            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Insert")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If (Me.iId = Guid.Empty) Then
                        .AddWithValue("@ServiceOrderId", System.DBNull.Value)
                    Else
                        .AddWithValue("@ServiceOrderId", Me.iId)
                    End If
                    If (Me.iCustomerId = Guid.Empty) Then
                        .AddWithValue("@CustomerId", System.DBNull.Value)
                    Else
                        .AddWithValue("@CustomerId", Me.iCustomerId)
                    End If
                    If (Me.iOrderNumber = String.Empty) Then
                        .AddWithValue("@OrderNumber", System.DBNull.Value)
                    Else
                        .AddWithValue("@OrderNumber", Me.iOrderNumber)
                    End If
                    If (Me.iOrderReceivedDate = DateTime.MinValue) Then
                        .AddWithValue("@OrderReceivedDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@OrderReceivedDate", Me.iOrderReceivedDate)
                    End If
                    If (Me.iOrderCompletedDate = DateTime.MinValue) Then
                        .AddWithValue("@OrderCompletedDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@OrderCompletedDate", Me.iOrderCompletedDate)
                    End If
                    If (Me.iServiceEquipmentId = Guid.Empty) Then
                        .AddWithValue("@ServiceEquipmentId", System.DBNull.Value)
                    Else
                        .AddWithValue("@ServiceEquipmentId", Me.ServiceEquipmentId)
                    End If
                    If (Me.iServiceItemImieSerialNumber = String.Empty) Then
                        .AddWithValue("@iServiceItemImieSerialNumber", System.DBNull.Value)
                    Else
                        .AddWithValue("@iServiceItemImieSerialNumber", Me.iServiceItemImieSerialNumber)
                    End If
                    If (Me.iEstimatedDate = DateTime.MinValue) Then
                        .AddWithValue("@EstimatedDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@EstimatedDate", Me.iEstimatedDate)
                    End If
                    .AddWithValue("@EstimatedPrice", Me.iEstimatedPrice)
                    .AddWithValue("@RegistrationFee", Me.iRegistrationFee)
                    .AddWithValue("@ActualPrice", Me.iActualPrice)
                    If (Me.iEstimateApproved = String.Empty) Then
                        .AddWithValue("@EstimateApproved", System.DBNull.Value)
                    Else
                        .AddWithValue("@EstimateApproved", Me.iEstimateApproved)
                    End If
                    If (Me.iStatus = String.Empty) Then
                        .AddWithValue("@Status", System.DBNull.Value)
                    Else
                        .AddWithValue("@Status", Me.iStatus)
                    End If
                    If (Me.iInDepartment = String.Empty) Then
                        .AddWithValue("@InDepartment", System.DBNull.Value)
                    Else
                        .AddWithValue("@InDepartment", Me.iInDepartment)
                    End If
                    If (Me.iLMUserId = Guid.Empty) Then
                        .AddWithValue("@LMUserId", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMUserId", Me.iLMUserId)
                    End If
                    If (Me.iLMDate = DateTime.MinValue) Then
                        .AddWithValue("@LMDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMDate", Me.iLMDate)
                    End If
                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to insert, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function Update() As Boolean
            'sp_[TableName]Update
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Update")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If (Me.iId = Guid.Empty) Then
                        .AddWithValue("@ServiceOrderId", System.DBNull.Value)
                    Else
                        .AddWithValue("@ServiceOrderId", Me.iId)
                    End If
                    If (Me.iCustomerId = Guid.Empty) Then
                        .AddWithValue("@CustomerId", System.DBNull.Value)
                    Else
                        .AddWithValue("@CustomerId", Me.iCustomerId)
                    End If
                    If (Me.iOrderNumber = String.Empty) Then
                        .AddWithValue("@OrderNumber", System.DBNull.Value)
                    Else
                        .AddWithValue("@OrderNumber", Me.iOrderNumber)
                    End If
                    If (Me.iOrderReceivedDate = DateTime.MinValue) Then
                        .AddWithValue("@OrderReceivedDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@OrderReceivedDate", Me.iOrderReceivedDate)
                    End If
                    If (Me.iOrderCompletedDate = DateTime.MinValue) Then
                        .AddWithValue("@OrderCompletedDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@OrderCompletedDate", Me.iOrderCompletedDate)
                    End If
                    If (Me.iServiceEquipmentId = Guid.Empty) Then
                        .AddWithValue("@ServiceEquipmentId", System.DBNull.Value)
                    Else
                        .AddWithValue("@ServiceEquipmentId", Me.ServiceEquipmentId)
                    End If
                    If (Me.iServiceItemImieSerialNumber = String.Empty) Then
                        .AddWithValue("@iServiceItemImieSerialNumber", System.DBNull.Value)
                    Else
                        .AddWithValue("@iServiceItemImieSerialNumber", Me.iServiceItemImieSerialNumber)
                    End If
                    If (Me.iEstimatedDate = DateTime.MinValue) Then
                        .AddWithValue("@EstimatedDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@EstimatedDate", Me.iEstimatedDate)
                    End If
                    .AddWithValue("@EstimatedPrice", Me.iEstimatedPrice)
                    .AddWithValue("@RegistrationFee", Me.iRegistrationFee)
                    .AddWithValue("@ActualPrice", Me.iActualPrice)
                    If (Me.iEstimateApproved = String.Empty) Then
                        .AddWithValue("@EstimateApproved", System.DBNull.Value)
                    Else
                        .AddWithValue("@EstimateApproved", Me.iEstimateApproved)
                    End If
                    If (Me.iStatus = String.Empty) Then
                        .AddWithValue("@Status", System.DBNull.Value)
                    Else
                        .AddWithValue("@Status", Me.iStatus)
                    End If
                    If (Me.iInDepartment = String.Empty) Then
                        .AddWithValue("@InDepartment", System.DBNull.Value)
                    Else
                        .AddWithValue("@InDepartment", Me.iInDepartment)
                    End If
                    If (Me.iLMUserId = Guid.Empty) Then
                        .AddWithValue("@LMUserId", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMUserId", Me.iLMUserId)
                    End If
                    If (Me.iLMDate = DateTime.MinValue) Then
                        .AddWithValue("@LMDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMDate", Me.iLMDate)
                    End If
                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to update, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function ValidateOnDelete() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnInsert() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnUpdate() As Boolean
            Return True
        End Function
#End Region

#Region "Overrides"
        Public Overrides Function ToString() As String
            Return Me.iOrderNumber
        End Function
        Public Overrides Function ToDetailsListBoxItem() As WindowsExtendedControls.DetailListBoxItem
            Return New WindowsExtendedControls.DetailListBoxItem(Me.iOrderNumber)
        End Function
#End Region

    End Class

    Public Class ServiceOrderTypeCollection
        Inherits DBTableObjectCollectionBase

        Public DataFilters As New DataFilterTypeCollection(Of ServiceOrderTypeFields)(Me.iTablename.ToString)
        Public DataSorters As New DataSorterTypeCollection(Of ServiceOrderTypeFields)(Me.iTableName.ToString)

        Public Sub New()
            MyBase.New(TableNamesEnum.SVC_ServiceOrders)
        End Sub

        Default Public Property Item(ByVal Index As Integer) As ServiceOrderType
            Get
                Return CType(Me.List.Item(Index), ServiceOrderType)
            End Get
            Set(ByVal value As ServiceOrderType)
                Me.List.Item(Index) = value
            End Set
        End Property

        Protected Friend Overrides Function Load() As Boolean
            Return Me.Load("SELECT * FROM " & Me.iTableName.ToString & Me.DataFilters.GetSQL & Me.DataSorters.GetSQL)
        End Function

        Protected Friend Overloads Function Load(ByVal CustomFilter As String) As Boolean
            'Get data from DB
            Dim cmd As New SqlCommand
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            Dim dt As DataTable
            Try
                cmd.CommandText = CustomFilter
                dt = Utility.GetList(cmd)
            Catch ex As Exception
                Throw ex
            End Try
            'Clear exising list
            Dim row As DataRow
            List.Clear()

            'Build new list
            Dim typeObject As ServiceOrderType

            For Each row In dt.Rows
                typeObject = New ServiceOrderType()
                typeObject.iConnection = Me.iConnection
                typeObject.iTransaction = Me.iTransaction
                If typeObject.Load(row) = True Then Me.List.Add(typeObject)
            Next
            Return True
        End Function

        Public Overloads Sub Sort(ByVal propertyToSort As ServiceOrderTypeFields, ByVal sortOrder As SortOrder)
            ipropertyToSort = propertyToSort.ToString
            isortOrder = sortOrder
            Me.InnerList.Sort(Me)
        End Sub

    End Class

#Region "Enumerations"       'Enumerations
    Public Enum ServiceOrderTypeFields
        ServiceOrderId
        CustomerId
        OrderNumber
        OrderReceivedDate
        OrderCompletedDate
        ServiceEquipmentId
        ServiceItemImieSerialNumber
        EstimatedDate
        EstimatedPrice
        RegistrationFee
        ActualPrice
        EstimateApproved
        Status
        LMUserId
        LMDate

    End Enum
#End Region

End Namespace